﻿using System.Linq;
using System.Data.Linq;
using System.Collections.Generic;

namespace UniMana.Data
{
    public static class LeadershipRelationshipData
    {
        /// <summary>
        /// Get data
        /// </summary>
        /// <param name="nLeadershipRelationship"></param>
        /// <returns></returns>

        public static IQueryable GetData(this Table<LeadershipRelationship> nLeadershipRelationship)
        {
            var values = from t in nLeadershipRelationship
                         orderby t.ApproveLevel ascending
                         select t;
            return values;
        }

        /// <summary>
        /// Lấy mã chi tiết phân quyền lãnh đạo
        /// </summary>
        /// <param name="nLeadershipRelationship"></param>
        /// <param name="m_strPersonnel_PersonnelKey"></param>
        /// <returns></returns>

        public static int ReturnLeadershipRelationshipKey(this Table<LeadershipRelationship> nLeadershipRelationship,
            string m_strPersonnel_PersonnelKey)
        {
            var values = (from t in nLeadershipRelationship
                          orderby t.LeadershipRelationshipKey descending
                          where t.Personnel_PersonnelKey.ToString() == m_strPersonnel_PersonnelKey
                          select t).FirstOrDefault();
            //if (values.LeadershipRelationshipKey != null)
            //    return int.Parse(values.LeadershipRelationshipKey.ToString());
            return 0;
        }

        /// <summary>
        /// Đếm số nhánh con của node
        /// </summary>
        /// <param name="nLeadershipRelationship"></param>
        /// <param name="m_iApproveLevel"></param>
        /// <param name="sum"></param>
        /// <returns></returns>

        public static int ReturnCountLeadershipRelationshipKey(this Table<LeadershipRelationship> nLeadershipRelationship,
            int m_iApproveLevel,
            int sum = 0)
        {
            var values = from t in nLeadershipRelationship
                         where t.ApproveLevel == m_iApproveLevel
                         select t;
            if (values.Count() > 0)
                return values.Count();
            return 0;
        }

        public static int ReturnRelationshipKeyWithApproveLevel(this Table<LeadershipRelationship> nLeadershipRelationship,
            int m_iApproveLevel)
        {
            var values = (from t in nLeadershipRelationship
                         where t.ApproveLevel == m_iApproveLevel
                         select t).Take(1);
            return int.Parse(values.FirstOrDefault().LeadershipRelationshipKey.ToString());
        }
        /// <summary>
        /// Kiểm tra thành viên thêm vào nhánh phân quyền
        /// </summary>
        /// <param name="nLeadershipRelationship"></param>
        /// <param name="m_iNameNode"></param>
        /// <param name="m_strPersonnal"></param>
        /// <returns></returns>

        public static bool CheckInsert(this Table<LeadershipRelationship> nLeadershipRelationship,
            int m_iNameNodeParent,
            string m_strPersonnal)
        {
            var values = from t in nLeadershipRelationship
                         where t.Personnel_PersonnelKey.ToString() == m_strPersonnal &&
                         t.ApproveLevel == m_iNameNodeParent
                         select t;
            if (values.Count() > 0)
                return false;
            else
                return true;
        }



        // Thêm mới

        //public static IQueryable GetListCensorshipWithLeadershipRelationshipKey(this Table<LeadershipRelationship> nLeadershipRelationship,
        //    int m_iLeadershipRelationshipKey)
        //{
        //    var values = from t in nLeadershipRelationship
        //                 where t.LeadershipRelationshipKey == m_iLeadershipRelationshipKey
        //                 select new
        //                 {
        //                     t.ApproveLevel
        //                 };
        //    return values;
        //}

        public static IQueryable GetListCensorshipWithLeadershipRelationshipKey(this Table<LeadershipRelationship> nLeadershipRelationship,
            Table<Personnel_Personnel> nPersonnel_Personnel,
            List<int> listKey)
        {
            int count = 0;
            while (count != listKey.Count)
            {
                count = listKey.Count;
                var getApproveLevel = (from query in nLeadershipRelationship
                                       where listKey.Contains(query.LeadershipRelationshipKey)
                                       select query.ApproveLevel == null ? 0 : query.ApproveLevel).Distinct();
               
                List<int?> listSubKey = getApproveLevel.ToList<int?>();


                var getListSub = from query in nLeadershipRelationship
                                 where listSubKey.Contains(query.LeadershipRelationshipKey)
                                 select query.LeadershipRelationshipKey;

                List<int> listParentKey = getListSub.ToList<int>();
                for (int i = 0; i < listParentKey.Count; i++)
                {
                    if (listKey.IndexOf(listParentKey[i]) == -1)
                    {
                        listKey.Add(listParentKey[i]);
                    }
                }
            }
            var lastQuery = from t in nLeadershipRelationship
                            join t1 in nPersonnel_Personnel on t.Personnel_PersonnelKey equals t1.Personnel_PersonnelKey
                            //orderby t.ApproveLevel ascending
                            where listKey.Contains(t.LeadershipRelationshipKey)
                            select new
                            {
                                t.LeadershipRelationshipKey,
                                t.Personnel_PersonnelKey,
                                t1.ID,
                                t1.LastName,
                                t1.Name,
                                FullName = t1.LastName + " " + t1.Name,
                                t.ApproveLevel
                            };
            return lastQuery;
        }
    }
}
